# 1. change path to install dir in setwd(...)
# 2. source this file in R
setwd("/Users/klausmeier/Documents/Diplom/trunk/arbeit/sources/R/clustervalidation")

source("analyse.new.R")

# uncomment next line to show samples
# samples

# uncomment next line to show indices
# indices

# uncomment next line to show algos
# algos


# Helper function to calculate max and min values of indices
minmaxOfIndex<-function(datass,idxs=c("iim")) { 
	result<-list()
	minmaxs<-list()
	for(d in names(datass)) 
		for(a in names(datass[[d]])) 
			for(i in names(datass[[d]][[a]]$indices)) 
				if(i %in% idxs){ 
					key=paste(d,a,sep="-")
					result= c(result,list(min=min(datass[[d]][[a]]$indices[[i]]),max=max(datass[[d]][[a]]$indices[[i]])) )
				}
	return(result);
}

# Run a full analyse of all samples with all algos, params and indices
if(T) {
	# result: analysed datasets  
	adss<-list() 
	# analyse all samples with all algos using


s = samples[[1]] 
# uncomment to run with all samples
#	for(s in samples[1]) 
		adss[[s$name]] =
			  analyseDataSet(s$d,s$rp,s$name
							, algos
							, get.notNormaliziedIndices( indices ) # unnormalizied indices
							, T # calculate G2 und G3 Indices
							)
    # Save results to file
	save(adss,file="adss.2un.r.dat")

    # plotAllIndices in a diagramm and save this in files per algo
	for(ads in adss) for( a in algos) {
		plotAllIndices(ads,a$name ); 
	 	dev.copy2eps(file=paste("ana2un",ads$datasetName,a$name,".eps",sep="_")); 
	}
	ais <-list();	for(i in names(indices)) for(ads in adss) for( a in algos) {
 		ais[[i]] <-c( ais[[i]] , ads[[a$name]]$indices[[i]] )
 	}
 	indexMinMax <- list()
 	for(i in names(ais)) {
 		indexMinMax[[i]]<-list(min=min(ais[[i]]),max=max(ais[[i]]))
 	}
}


if(F) {
#	source("analyse.new.R")
#adss2=removeindices(adss,c("SC","G3"))	
source("analyse.new.R")
load(file="8d/ana-8d.dat") #adss2 <-- file="ana-8d.dat"

summary(adss2)
summary(adss2$Ruspini)
summary(adss2$Ruspini$PAM$indices)

a="DBScan";d="5_Zentren";
quartz(width=9,height=4.5);
cex = 1.0
plotSummary(adss2[[d]],a,main=paste("Indexgraphen für Datensatz",adss2[[d]]$datasetName,"geclustert mit",a))
dev.copy2eps(file=paste("images/idxes-",d,"-",a,".eps",sep=""))


minmaxs=minmaxOfIdx(adss2)

#sort(s$Ruspini$PAM$indices$Gamma,index.return=T,decreasing=T)
#ls=list(x=s$x[1:4],ix=s$ix[1:4])

quartz(width=9,height=13);
lwd=1.5;
cex=1.2;
par(mfrow=c(3,1),mar=c(6,6,6,6),oma=c(6,6,6,6)) ; 
for(d in names(adss2)) { 
		for(a in names(adss2[[d]])) if(! a %in% c("datasetName","dataset")) plotSummary(adss2[[d]],a,main=paste(adss2[[d]]$datasetName,a)) ; dev.copy2eps(file=paste(d,"eps",sep=".")) 
}
}


# example
if(F) {
	if(F) {  #construct ?
		adss<-list() 
		for(s in samples)
			#if(s$name == "Ruspini") 
			{
		 	adss[[s$name]] = 
			  	analyseDataSet( s$d, s$rp, s$name
								, list(algos$PAM,algos$DBScan,algos$ChiWhi)
								, indices, F)
			}
		adss2=removeindices(adss,c("SC","G3","iim"))	
		save(adss2,file="ana6-8d.dat")
	} else { # load
		load("8d/ana6-8d.dat")
		adss2=removeindices(adss2,c("SC","G3","iim"))
	}
	
	# create specific indexgraphs
	{
		adfns=list(
		#list(a="DBScan",d="5_Zentren", fn="ana-d2-dbscan.eps"),
		#list(a="PAM",d="Ruspini", fn="ana-d1-pam.eps"),
		#list(a="DBScan",d="Gleichverteilt", fn="ana-d5-dbscan.eps"),
		#list(a="ChiWhi",d="2_Zigarren", fn="ana-d3-chiwhi.eps"),
		#list(a="ChiWhi",d="Gleichverteilt", fn="ana-d5-chiwhi.eps"),
		#list(a="ChiWhi",d="Ruspini", fn="ana-d1-chiwhi.eps")
		list(a="ChiWhi",d="Stroop", fn="ana-d7-chiwhi.eps")
		)
		for(adfn in adfns) {
			a=adfn$a;d=adfn$d;fn=adfn$fn
			quartz(width=9,height=4.5);
			cex = 1.0
			plotSummary(adss2[[d]],a,main=paste("Indexgraphen für Datensatz",adss2[[d]]$datasetName,"geclustert mit",a))
			dev.copy2eps(file=paste("images",fn,sep="/"))
		}
	}
	
	# 3D Results
	{
		# set white background
		rgl.bg( sphere = FALSE, fogtype = "none", color=c("white","black"), back="lines")
		
		# show best dbscan partition for stroop dateset
		rp=adss2[["Stroop"]][["DBScan"]]$partitions[19][[1]];d=adss2[["Stroop"]]$dataset$d;
		showSpheres3d(d,rp)
		rgl.snapshot("images/3d-stroop-best19-dbscan.png")
		
		# reference partition
		rp=adss2[["Stroop"]]$dataset$rp;d=adss2[["Stroop"]]$dataset$d
		showSpheres3d(d,rp)
		rgl.snapshot("images/3d-stroop-refpart.png")

		# manuelly convert to tiffs and crop. 
	}
	
	quartz(width=9,height=13);

	lwd=1.5;
	cex=1.0;
	par(mfrow=c(3,1),mar=c(6,6,6,6),oma=c(6,6,6,6)) ; 
	for(d in names(adss2)) { 
		for(a in names(adss2[[d]])) 
			if(! a %in% c("datasetName","dataset")) 
				plotSummary(adss2[[d]],a,main=paste(adss2[[d]]$datasetName,a)) ; 
		dev.copy2eps(file=paste(d,"eps",sep=".")) 
	}
	
#	for(ads in adss2) for( a in algos) {
#		plotAllIndices(ads,a$name ); 
#	 	dev.copy2eps(file=paste("ana6",ads$datasetName,a$name,".eps",sep="_")); 
#	}


	mi<-function(xs) {rs=sort(xs,T,index.return=T);list(x=round(rs$x[1],3),ix=rs$ix[1])}
	mm<-list();j=1;
	for(d in names(adss2)) 
		for(a in names(adss2[[d]])[1:3]) {
			is=adss2[[d]][[a]]$indices;
			mm[[j]]=data.frame(Dataset=sub("ö","oe",sub("_"," ",d)),Algo=a,
					Gamma=mi(is$Gamma)$x,
					dunn=mi(is$dunn)$x,
					gamma=mi(is$gamma)$x,
					rand=mi(is$rand)$x,
					hits=paste(  
						     ifelse(mi(is$Gamma)$ix ==mi(is$rand)$ix,"G","" ),
							 ifelse(mi(is$dunn)$ix  ==mi(is$rand)$ix,"d","" ),
							 ifelse(mi(is$gamma)$ix ==mi(is$rand)$ix,"g","" )
							 )
													 
							 );
			j=j+1
		}
	rs=list();for(c in names(mm[[1]])) rs[[c]]=map(mm,c)
	latex(rs,file="3.tex")

}
